package com.example.liang.basamovie.json;
import com.example.liang.basamovie.bean.Recommend;
import com.example.liang.basamovie.bean.Recommend2;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class RecommendJson {
    public static List<Object> getRecommend(String info){
        try {
            JSONObject object = new JSONObject(info);
            JSONArray array = object.getJSONArray("infoList");
            List<Object> list=new ArrayList<>();
                for (int i = 0; i < array.length(); i++) {
                    JSONObject object2 = array.getJSONObject(i);
                    String objectType = object2.optString("objectType");
                    if (objectType.equals("1")) {
                        JSONObject object1 = object2.getJSONObject("object");
                        Recommend recommend = new Recommend();
                        recommend.setType(object1.optString("keyword"));
                        recommend.setTitle(object1.optString("title"));
                        recommend.setContentUrl(object1.optString("articleContentUrl"));
                        recommend.setImage_url(object1.optString("imgUrl"));
                        JSONObject object3 = object1.getJSONObject("articleSource");
                        recommend.setNickname(object3.optString("nickname"));
                        recommend.setData(object1.optLong("recommendDate"));
                        list.add(recommend);
                    } else if (objectType.equals("2")) {
                        JSONObject object4 = object2.getJSONObject("object");
                        JSONObject object5 = object4.getJSONObject("shareUrl");
                        JSONObject object6 = object4.getJSONObject("articleSource");
                        Recommend2 recommend2 = new Recommend2();
                        recommend2.setVideotime(object4.optString("videoTime"));
                        recommend2.setCreatetime(object4.optLong("createDate"));
                        recommend2.setTitle(object5.optString("title"));
                        recommend2.setDescription(object5.optString("description"));
                        recommend2.setUrl(object5.optString("url"));
                        recommend2.setImgurl(object5.optString("imageUrlSrc"));
                        recommend2.setNickname(object6.optString("nickname"));
                        recommend2.setVideoUrl(object4.optString("videoUrl"));
                        list.add(recommend2);
                    }
                }
            return list;

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}
